﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataRowCompareTbl.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The data row compare tbl.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.Common.Comparer
{
    using System;
    using System.Data;
    using DataAccess;
    using Helper;
    using Model;

    /// <summary>
    /// The data row compare tbl.
    /// </summary>
    public class DataRowCompareTbl : DataRowCompare
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DataRowCompareTbl"/> class.
        /// </summary>
        /// <param name="errorMessageTables">
        /// The dt error msg.
        /// </param>
        /// <param name="dataSetDiff">
        /// The dt diff.
        /// </param>
        /// <param name="databaseInfoBase">
        /// The database infobase obj.
        /// </param>
        /// <param name="regMatch">
        /// The reg match.
        /// </param>
        public DataRowCompareTbl(ErrorMessageTables errorMessageTables, DataSetDiff dataSetDiff, DatabaseInfoBase databaseInfoBase, RegMatch regMatch)
            : base(errorMessageTables, dataSetDiff, databaseInfoBase, regMatch)
        {
        }

        /// <summary>
        /// The compare row.
        /// </summary>
        /// <param name="row1">
        /// The row 1.
        /// </param>
        /// <param name="row2">
        /// The row 2.
        /// </param>
        /// <param name="objectType">
        /// The object type.
        /// </param>
        /// <param name="startfrom">
        /// The startfrom.
        /// </param>
        /// <param name="owner">
        /// The owner.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="field">
        /// The field.
        /// </param>
        /// <param name="indexName">
        /// The index name.
        /// </param>
        /// <param name="objectId">
        /// The object id.
        /// </param>
        /// <returns>
        /// The compare row.
        /// </returns>
        public override string CompareRow(DataRow row1, DataRow row2, DatabaseObjectTypes objectType, int startfrom, string owner, string name, string field, string indexName, int objectId)
        {
            string errorMessages = string.Empty;
            int objType = this.ErrorMessageTables.FindIndexByName(DatabaseObjectDictionary.ObjectTypes[objectType]);

            // check first hashcode 
            if (row1.ItemArray.GetHashCode() != row2.ItemArray.GetHashCode())
            {
                int i = row1.ItemArray.Length;
                for (int j = startfrom; j < i; j++)
                {
                    // properties must be compare and 2 properties not equals
                    // so for example on the form unchecked field nullable checking
                    // we will not compare the fields
                    // BUGBUG: we need some cases for example defaults, and calculated fields
                    // BUGBUG WHITESPACE etc
                    // that we have to remove white spaces and remarks and others for
                    // stored porcedures,views,functions and triggers
                    if (
                        row1.Table.Columns[j].Caption != ComparererConstraint.Caption
                        &&
                        !this.CompareString(Convert.ToString(row1[j]), Convert.ToString(row2[j]), false, this.DatabaseInfoBase.DatabaseDataSets.CaseSensitive)
                        )
                    {
                        // properties not equals
                        // errormessage from errordataset
                        errorMessages += (errorMessages != string.Empty ? "," : string.Empty) +
                                         this.ErrorMessageTables.Tables[objType].Columns[row1.Table.Columns[j].ColumnName].Caption;

                        this.DtDiff.dbDiffDetails.AdddbDiffDetailsRow(
                            owner,
                            name,
                            field,
                            string.Empty, // indexName
                            this.ErrorMessageTables.Tables[objType].Columns[row1.Table.Columns[j].ColumnName].ColumnName,
                            this.ErrorMessageTables.Tables[objType].Columns[row1.Table.Columns[j].ColumnName].Caption,
                            objectId,
                            Convert.ToString(row1[j] ?? "null"),
                            Convert.ToString(row2[j] ?? "null"),
                            objectType.ToString());
                    }
                }
            }

            return errorMessages;
        }
    }
}